<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>Firmware</title>
		<script>
			var model_nos = {}
            model_nos[""] = ["",]
			{% for main in model_nos %}
			model_nos["{{main}}"] = [{% for sub in model_nos[main] %}"{{sub}}",{% endfor %}]
			{% endfor %}
			
            var onModelMainNoChanged = function(mainSelectId, subSelectId) {
                var modelMainNosSelect = document.getElementById(mainSelectId)
                var modelSubNosSelect = document.getElementById(subSelectId)
				var modelSubNosSelectItems = modelSubNosSelect.children
				for(; modelSubNosSelectItems.length > 0;) {
					modelSubNosSelect.removeChild(modelSubNosSelectItems[0])
				}
				
				var modelMainNo = modelMainNosSelect.options[modelMainNosSelect.selectedIndex].value
				
				var modelSubNos = model_nos[modelMainNo]
				for(let i=0; i < modelSubNos.length; i++) {
					modelSubNoOption = document.createElement("option")
					modelSubNoOption.innerText = modelSubNos[i].toString()
					modelSubNoOption.value = modelSubNos[i].toString()
					modelSubNosSelect.appendChild(modelSubNoOption)
				}
            }
			
			window.onload = function() {
				onModelMainNoChanged('modelMainNosSelect', 'modelSubNosSelect')
			}

        </script>
		<style>
			.main {
				display: inline-flex;
				flex-direction: column;
				align-items: center;
			}
			.main table {
				width: 100%;
			}
			.main tr {
				width: 100%;
			}
			.main td:first-child {
				text-align: right;
			}
			.main input {
				min-width: 100px;
			}
			.main button {
				min-width: 100px;
			}
			.main select {
				width: 100%;
			}
		</style>
	</head>
	<body>
		<h1>Firmware</h1>
		<div class="main">
			<form action="" enctype="multipart/form-data" method="post">
				<table>
					<tr>
						<td>Model Main No:</td>
						<td><select id="modelMainNosSelect" name="main_no" onchange="onModelMainNoChanged('modelMainNosSelect', 'modelSubNosSelect')">
								<option value=""></option>
								{% for main in model_nos %}
								<option value="{{ main }}">{{ main }}</option>
								{% endfor %}
							</select>
						</td>
					</tr>
					<tr>
						<td>Model Sub No:</td>
						<td><select id="modelSubNosSelect" name="sub_no"></select></td>
					</tr>

					<tr>
						<td>Name: </td>
						<td><input type="text" name="name"></td>
					</tr>
					<tr>
						<td>Version: </td>
						<td><input type="text" name="version"></td>
					</tr>
					<tr>
						<td>Type: </td>
						<td><select name="type">
								<option value=""></option>
								{% for type in firmware_types %}
								<option value="{{ type }}">{{ type }}</option>
								{% endfor %}
							</select></td>
					</tr>
					<tr>
						<td>Status:</td>
						<td><select name="status">
								<option value=""></option>
								{% for status in firmware_statuses %}
								<option value="{{ status }}">{{ status }}</option>
								{% endfor %}
							</select></td>
					</tr>
				</table>
				<div align="right"><input type="submit" name="action" value="query"></div>
				{% if firmwares %}
				<table style="border: 1px solid black; border-collapse: collapse;">
					<!-- title row -->
					<tr>
						<th align="center" style="border: 1px solid black;"> Model </th>
						<th align="center" style="border: 1px solid black;"> FW Name </th>
						<th align="center" style="border: 1px solid black;"> FW version </th>
						<th align="center" style="border: 1px solid black;"> FW type </th>
						<th align="center" style="border: 1px solid black;"> FW status </th>
						<th align="center" style="border: 1px solid black;"> Operation </th>
					</tr>
				{% for firmware in firmwares %}
					<!-- data rows -->
					<tr>
						<td align="center" style="border: 1px solid black; align: center;"> {{firmware['main_no']}}_{{firmware['sub_no']}}</td>
						<td align="center" style="border: 1px solid black;"> {{firmware['name']}} </td>
						<td align="center" style="border: 1px solid black;"> {{firmware['version']}} </td>
						<td align="center" style="border: 1px solid black;"> {{firmware['type']}} </td>
						<td align="center" style="border: 1px solid black;"> {{firmware['status']}} </td>
						<td align="center" style="border: 1px solid black;">
							<button name="action" value="delete:{{firmware['id']}}">Delete</button>
							<button name="action" value="download:{{firmware['id']}}">Download</button>
						</td>
					</tr>
				{% endfor %}
				{% endif %}
				</table>
				<div style="width: 100%; display: flex; align-content: stretch; margin-top: 10px;">
					<span style="flex-grow: 0">File Path: </span>
					<input id="file_upload" style="flex-grow: 1" type="file" name="file" value={{ uploaded_file }}>
					<input style="flex-grow: 0" type="submit" name="action" value="upload">
				</div>
			</form>
		</div>
		{% for message in get_flashed_messages() %}
		<div> {{ message }}</div>
		{% endfor %}

	</body>
</html>
